<?php


namespace Question\Controller;

use Think\Controller;

/**
 * Class UsercenterController
 * @package Question\Controller
 * @郑钟良
 */
class UsercenterController extends Controller
{

    protected $mUid;
    public function _initialize()
    {
        $this->mUid=I('uid',0,'intval')?I('uid',0,'intval'):is_login();
        $this->assign('uid',$this->mUid);
        $this->setTitle('问题');
    }

    public function publish($page=1)
    {
        $map=array('status'=>1,'uid'=>$this->mUid);
        $questionList = D('Question/Question')->getList($map, $totalCount, $page);
        $this->assign('questionList', $questionList);
        $this->assign('totalCount', $totalCount);
        $this->setTitle('发布的问题主页');
        $this->assign('tabHash','question');
        $this->display();
    }

    public function answer($page=1){
        $answer_list = D('Question/QuestionAnswer')->answeredQuestion($this->mUid,$page,$totalCount);
        $this->assign('answer_list', $answer_list);
        $this->assign('totalCount', $totalCount);
        $this->setTitle('回答的问题主页');
        $this->assign('tabHash','question');
        $this->display();
    }
    public function ask()
    {
        $question_id = I('question_id', 0, 'intval');
        $isEdit = $question_id ? 1 : 0;
        if (IS_POST) {
            if ($isEdit) {
                $result = $this->addQuestion($isEdit);
                if ($result) {
                    $this->success('问题编辑成功！', U('question/index/detail', array('id' => $result)));
                } else {
                    $this->error('问题编辑失败！');
                }
            } else {
                $result = $this->addQuestion($question_id);
                if ($result) {
                    $this->success('问题发布成功！', U('question/index/detail', array('id' => $result)));
                } else {
                    $this->error('问题发布失败！');
                }
            }
        } else {
            $typeList = D('Help/HelpType')->where(array('status' => 1))->field('id,title')->select();
            $this->assign('typeList', $typeList);
            if ($isEdit) {
                $question = D('Question/question')->getQuestion($question_id);
                $this->assign('question', $question);
            }
            $this->assign('isEdit',$isEdit);
            $this->display();
        }
    }

    private function addQuestion($isEdit = 0)
    {
        if (!is_login()) {
            $this->error('请先登录！');
        }
        if ($isEdit) {
            $data['id'] = I('post.question_id', 0, 'intval');
        }
        $aTitle = $data['title'] = I('post.title', '', 'op_t');
        $aContent = $data['content'] = I('post.explain', '', 'op_h');
        $aCoverId = $data['cover_id'] = I('post.cover_id', 0, 'intval');
        $data['type'] = I('post.type', '0', 'intval');
        if (!$aCoverId) {
            $this->error('请选择封面');
        }
        if (mb_strlen($aTitle, 'utf-8') < 4 || mb_strlen($aTitle, 'utf-8') > 20) {
            $this->error('标题长度必须在4-20个字之间');
        }
        if (mb_strlen($aContent, 'utf-8') == 0) {
            $this->error('描述不能为空');
        }
        if ($isEdit) {
            $result = D('Question/Question')->editQuestion($data);
        } else {
            $data['uid'] = is_login();
            $result = D('Question/Question')->createQuestion($data);
            if($result){
                $addon_config = get_addon_config('SyncLogin');
                if(in_array('Sina',$addon_config['type'])){
                    $server_host = "http://" . $_SERVER ['HTTP_HOST'];
                    $weibo_content = '我在'.C('WEB_SITE').'发布了问题：'.$aTitle.'，点击查看'.$server_host.U('question/index/detail', array('id' => $result));
                    sync_send_weibo($weibo_content,'sina');
                }
            }
        }
        return $result;
    }

    public function personalAnswer(){
        $aQuestionId=$map['question_id']=I('post.question_id',0,'intval');
        $aUid=$map['uid']=I('post.uid',0,'intval');
        $msg['status']=0;
        if(!$aQuestionId||!$aUid){
            $msg['info']='参数错误！';
            $this->ajaxReturn($msg);
        }
        $answerList=D('Question/QuestionAnswer')->getAnswerList($map,0,$totalCount);
        if(count($answerList)){
            $msg['status']=1;
            $this->assign('answerList',$answerList);
            $msg['html']=$this->fetch('Public/_personalanswer');
        }else{
            $msg['info']='获取数据失败！';
        }
        $this->ajaxReturn($msg);
    }
}